home *** CD-ROM | disk | FTP | other *** search
- /*
- * File: pnumbers.c
- * Creator: Gregg Jennings
- * Version: 1.0 February 1991
- * Purpose: display formatted numbers to the console
- */
-
- #include <stdio.h>
-
- void lpn(unsigned int n, int length, int base);
- void pn(unsigned int n, int base);
- int getlen(unsigned int n, int base);
-
- /* some MACRO examples */
-
- #define paddr(n) lpn(n,4,16),putch(':')
- #define phex(n) lpn(n,2,16)
-
- #define newline putch('\n'),putch('\r')
-
- void main()
- {
- int i;
-
- lpn(123,16,2); /* display 123 dec in binary
- as if printf("%016b",123) would work */
- newline;
- pn(123,2); /* display 123 dec in binary */
- newline;
- pn(123,8); /* octal */
- newline;
- pn('\173',10); /* 173 oct in decimal */
- newline;
- paddr(123); /* display an address */
- newline;
- phex('A'); /* display a hex value */
- newline;
-
- /* display the first 26 octal numbers */
- for (i=0;i<26;i++) {
- pn(i,8);
- putch(' ');
- }
- newline;
-
- /* display the first 21 base 3 numbers */
- for (i=0;i<21;i++) {
- pn(i,3);
- putch(' ');
- }
- newline;
- }
-
- /*
- * display a number to the console in any base from
- * 2 to 36, ASCII character set only
- */
-
- void pn(unsigned int n, int base)
- {
- if (n/base)
- pn(n/base,base);
- putch((n%base>9) ? n%base+'A'-10 : n%base+'0');
- }
-
- /*
- * display a number to the console with leading zeros
- * and a certain length, and any base from 2 to 36
- */
-
- void lpn(unsigned int n, int length, int base)
- {
- register int t;
- for (t=length-getlen(n,base);t>0;t--)
- putch('0');
- pn(n,base);
- }
-
- /*
- * get the display length (number of digits) of a
- * number, i.e. 100 dec returns 3, 64 hex (100 dec)
- * returns 2
- *
- * used by lpn()
- */
-
- int getlen(unsigned int n, int base)
- {
- int j;
-
- for (j=1;n>=base;n/=base,++j)
- ;
- return(j);
- }
-